package com.xiaoliang.dao.impl;

import com.xiaoliang.dao.ProductDao;
import com.xiaoliang.entry.Product;
import com.xiaoliang.tools.JDBCUtils.JDBCUtils;

import java.util.List;

/**
 * @author XIAOLIANG
 * @ClassName ProductDaoImpl.java
 * @Description TODO
 * @createTime 2021年02月25日 20:16:00
 */
public class ProductDaoImpl implements ProductDao {
	/**
	 * 增加 一条数据
	 *
	 * @param product
	 * @return
	 */
	@Override
	public int insert(Product product) {
		return JDBCUtils.update("insert into product values(null,?,?,?,?,?)",product.getName(),product.getPrice(),product.getAccount(),product.getStatus(),product.getDescription());
	}

	/**
	 * 根据 id 删除一条数据
	 *
	 * @param id
	 * @return
	 */
	@Override
	public int deleteByID(Integer id) {
		return JDBCUtils.update("delete from product where id = ? ",id);
	}

	/**
	 * 修改 一条数据
	 *
	 * @param product
	 * @return
	 */
	@Override
	public int update(Product product) {
		return JDBCUtils.update("update product set name = ? ,price = ? ,account =?,status = ? ,description=? where id = ?",product.getName(),product.getPrice(),product.getAccount(),product.getStatus(),product.getDescription(),product.getId());

	}

	/**
	 * 根据id 查询一条数据
	 *
	 * @param id
	 * @return
	 */
	@Override
	public Product selectById(Integer id) {
		return JDBCUtils.queryOne("select * from product where id =?", Product.class,id);
	}

	/**
	 * 查询所有的数据
	 *
	 * @return
	 */
	@Override
	public List<Product> selectAll() {
		return JDBCUtils.query("select * from product",Product.class);
	}

	@Override
	public List<Product> selectPage(Integer pageNum, Integer pageSize) {
		return JDBCUtils.query("select * from product limit ?,?",Product.class,(pageNum-1)*pageSize,pageSize);
	}

	@Override
	public Integer selectCount() {
		return JDBCUtils.count("select count(*) from product");
	}

	@Override
	public Integer selectCountByCondition(String productName, String operation, String price) {
		return JDBCUtils.count("select count(id) from product where name like ? and price"+operation+"?",productName,price);
	}

	@Override
	public List<Product> selectByConditionAndPage(String productName, String operation, String number, Integer pageNum, Integer pageSize) {
		return JDBCUtils.query("select * from product where name like ? and price"+operation+"? limit ?,?",Product.class,productName,number,(pageNum-1)*pageSize,pageSize);
	}

}
